/******************************************************************************
 * XWiki.JavaScriptExtension[1]: modified exhibit-api.js: must define JS vars 
 * 'exhibit_views' and 'exhibit_gmapkey' prior to calling $xwiki.jsx.use().
 * Original from http://static.simile.mit.edu/exhibit/api-2.0/exhibit-api.js .
 * Copyright (C) 2009-2011, Niels Mayer. All Rights Reserved. http://nielsmayer.com
 ******************************************************************************/
/* note: vars exhibit_views, exhibit_gmapkey defined/set by exhibit_xwiki_init() */
(function() {
  var useLocalResources = true; // NPM: set to true to use ../../api/ 
  var loadMe = function() {
    if (typeof window.Exhibit != "undefined") {
      return;
    }
    window.Exhibit = {
      version:    "NPM wrapped trunk pre 2.3.0",
      loaded:     false,
      params:     { bundle: true, authenticated: false, autoCreate: false, safe: false, views: exhibit_views, gmapkey: exhibit_gmapkey },
      namespace:  "http://simile.mit.edu/2006/11/exhibit#",
      importers:  {},
      locales:    [ "en" ]
    };
    var javascriptFiles = [
			   "exhibit.js",
			   "persistence.js",
			   "authentication.js",
			   "util/set.js",
			   "util/util.js",
			   "util/settings.js",
			   "util/views.js",
			   "util/facets.js",
			   "util/coders.js",
			   "data/database.js",
			   "data/expression.js",
			   "data/expression-parser.js",
			   "data/functions.js",
			   "data/controls.js",
			   "data/collection.js",
			   "data/importers/authenticated-importer.js",
			   "data/importers/exhibit-json-importer.js",
			   "data/importers/html-table-importer.js",
			   "data/importers/jsonp-importer.js",
			   "data/importers/babel-based-importer.js",
			   "data/importers/rdfa-importer.js",
			   "data/exporters/rdf-xml-exporter.js",
			   "data/exporters/semantic-wikitext-exporter.js",
			   "data/exporters/exhibit-json-exporter.js",
			   "data/exporters/tsv-exporter.js",
			   "data/exporters/bibtex-exporter.js",
			   "data/exporters/facet-selection-exporter.js",
			   "ui/ui.js",
			   "ui/ui-context.js",
			   "ui/lens.js",
			   "ui/format-parser.js",
			   "ui/formatter.js",
			   "ui/coordinator.js",
			   "ui/facets/list-facet.js",
			   "ui/facets/numeric-range-facet.js",
			   "ui/facets/text-search-facet.js",
			   "ui/facets/cloud-facet.js",
			   "ui/facets/hierarchical-facet.js",
			   "ui/facets/image-facet.js",
			   "ui/facets/slider-facet.js",
			   "ui/facets/slider.js",
			   "ui/facets/alpha-range-facet.js",
			   "ui/coders/color-coder.js",
			   "ui/coders/default-color-coder.js",
			   "ui/coders/color-gradient-coder.js",
			   "ui/coders/size-coder.js",
			   "ui/coders/size-gradient-coder.js",
			   "ui/coders/icon-coder.js",
			   "ui/widgets/logo.js",
			   "ui/widgets/collection-summary-widget.js",
			   "ui/widgets/resizable-div-widget.js",
			   "ui/widgets/legend-widget.js",
			   "ui/widgets/legend-gradient-widget.js",
			   "ui/widgets/option-widget.js",
			   "ui/widgets/toolbox-widget.js",
			   "ui/views/view-panel.js",
			   "ui/views/ordered-view-frame.js",
			   "ui/views/tile-view.js",
			   "ui/views/thumbnail-view.js",
			   "ui/views/tabular-view.js",
			   "ui/views/html-view.js"
			   ];
    var cssFiles = [
		    "exhibit.css",
		    "browse-panel.css",
		    "lens.css",
		    "util/facets.css",
		    "util/views.css",
		    "widgets/collection-summary-widget.css",
		    "widgets/resizable-div-widget.css",
		    "widgets/legend-widget.css",
		    "widgets/option-widget.css",
		    "widgets/toolbox-widget.css",
		    "views/view-panel.css",
		    "views/tile-view.css",
		    "views/thumbnail-view.css",
		    "views/tabular-view.css"
		    ];
    var includeMap = false;
    var includeTimeline = false;
    var includeTimeplot = false; /* NPM: added 10/26/10 attempting to fix problem loading timeplots after change to XWiki 2.4 */
    var defaultClientLocales = ("language" in navigator ? navigator.language : navigator.browserLanguage).split(";");
    for (var l = 0; l < defaultClientLocales.length; l++) {
      var locale = defaultClientLocales[l];
      if (locale != "en" && locale != "en-US") {
	var segments = locale.split("-");
	if (segments.length > 1 && segments[0] != "en" && segments[0] != "en-US") {
	  Exhibit.locales.push(segments[0]);
	}
	Exhibit.locales.push(locale);
      }
    }
    var paramTypes = { bundle:Boolean, js:Array, css:Array, autoCreate:Boolean, safe:Boolean };
    if (typeof Exhibit_urlPrefix == "string") {
      Exhibit.urlPrefix = Exhibit_urlPrefix;
    } else {
      Exhibit.urlPrefix  = "http://api.simile-widgets.org/exhibit/2.2.0/";
    }
    if (useLocalResources) {
      Exhibit.urlPrefix = "../../api/"; // NPM: for qtzibit
    }
    // NPM: added...
    if ("Exhibit_parameters" in window) {
      SimileAjax.parseURLParameters(Exhibit_parameters,
				    Exhibit.params,
				    paramTypes);
    }
    if (Exhibit.params.locale) { // ISO-639 language codes,
      // optional ISO-3166 country codes (2 characters)
      if ((Exhibit.params.locale != "en") && (locale != "en-US")) {
	var segments = Exhibit.params.locale.split("-");
	if (segments.length > 1 && segments[0] != "en" && segments[0] != "en-US") {
	  Exhibit.locales.push(segments[0]);
	}
	Exhibit.locales.push(Exhibit.params.locale);
      }
    }
    if (Exhibit.params.gmapkey) {
      includeMap = true;
    }
    if (Exhibit.params.views) {
      var views = Exhibit.params.views.split(",");
      for (var j = 0; j < views.length; j++) {
	var view = views[j];
	if (view == "timeline") {
	  includeTimeline = true;
	} else if (view == "map") {
	  includeMap = true;
	} else if (view == "timeplot") { /* NPM: added 10/26/10 attempting to fix problem loading timeplots after change to XWiki 2.4 */
	  includeTimeplot = true;
	}
      }
    }
    var scriptURLs = Exhibit.params.js || [];
    var cssURLs = Exhibit.params.css || [];
    /*
     *  Core scripts and styles
     */
    if (true) {
      scriptURLs.push(Exhibit.urlPrefix + "exhibit-bundle.js");
      cssURLs.push(Exhibit.urlPrefix + "exhibit-bundle.css");
    } else {
      SimileAjax.prefixURLs(scriptURLs, Exhibit.urlPrefix + "scripts/", javascriptFiles);
      SimileAjax.prefixURLs(cssURLs, Exhibit.urlPrefix + "styles/", cssFiles);
    }
    /*
     *  Localization
     */
    for (var i = 0; i < Exhibit.locales.length; i++) {
      scriptURLs.push(Exhibit.urlPrefix + "locales/" + Exhibit.locales[i] + "/locale.js");
    };
    if (Exhibit.params.callback) {
      window.SimileAjax_onLoad = function() {
	eval(Exhibit.params.callback + "()");
      }
    } else if (Exhibit.params.autoCreate) {
      scriptURLs.push(Exhibit.urlPrefix + "scripts/create.js");
    }
    /*
     *  Extensions (for backward compatibility)
     */
    if (includeTimeline) {
      scriptURLs.push(Exhibit.urlPrefix + "extensions/time/time-extension.js");
    }
    if (includeMap) {
      scriptURLs.push(Exhibit.urlPrefix + "extensions/map/map-extension.js");
    }
    if (includeTimeplot) { /* NPM added 10/26/10 to fix problems w/ timeplot not loading in Xwiki 2.4 */
      scriptURLs.push(Exhibit.urlPrefix + "extensions/timeplot/timeplot-extension.js");
    }

    SimileAjax.includeJavascriptFiles(document, "", scriptURLs);
    SimileAjax.includeCssFiles(document, "", cssURLs);
    Exhibit.loaded = true;
  };
  /*
   *  Load SimileAjax if it's not already loaded
   */
  if (typeof SimileAjax == "undefined") {
    window.SimileAjax_onLoad = loadMe;
    var url = useLocalResources ?
      "../../../ajax/api/simile-ajax-api.js" : // for qtzibit
      "http://api.simile-widgets.org/ajax/2.2.1/simile-ajax-api.js?bundle=true";
    var createScriptElement = function() {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.language = "JavaScript";
      script.src = url;
      document.getElementsByTagName("head")[0].appendChild(script);
    };
    if (document.body == null) {
      try {
	document.write("<script src='" + url + "' type='text/javascript'></script>");
      } catch (e) {
	createScriptElement();
      }
    } else {
      createScriptElement();
    }
  } else {
    loadMe();
  }
 })();
